﻿using StorageApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StorageApi.Controllers
{
    public class HomeController : Controller
    {
        JooWMSEntities db = new JooWMSEntities();

        public ActionResult UserList(string id)
        {
            //if (!Pd())
            //{
            //    return Content("<script>alert('请重新登录！');window.location='/admin/index'</script>");
            //}
            //var userlist = db.Admin.ToList();
            //ViewBag.userlist = userlist;
            if (Session["user"] != null)
            {
                return PartialView("_UserList");
            }
            else
            {
                return Content("<script>alert('请重新登录！');window.location='/admin/index'</script>");
            }
        }
        public ActionResult Index()
        {
            if (!Pd())
            {
                return Content("<script>alert('请重新登录！');window.location='/admin/index'</script>");
            }
            return View();
        }
        public bool Pd()
        {
            if (Session["user"] != null)
            {
                //获取角色和部门的数据
                var adminlist = db.SysRole.ToList();
                var dep = db.SysDepart.ToList();
                ViewBag.adminlist = adminlist;
                ViewBag.dep = dep;

                //获取资源名称数据
                var resource = db.SysResource.ToList();

                Dictionary<string, List<SysResource>> dr = new Dictionary<string, List<SysResource>>();
                foreach (var item in resource.Where(res => res.ParentNum == ""))
                {
                    dr.Add(item.ResName, resource.Where(r => r.ParentNum == item.ResNum).ToList());
                }
                ViewBag.dr = dr;
                return true;
            }
            return false;
        }
        public ActionResult updata(Admin admin)
        {
            Admin user = (Admin)Session["user"];
            user.RealName = admin.RealName;
            user.Email = admin.Email;
            user.Mobile = admin.Mobile;
            user.Phone = admin.Phone;
            db.Entry(user).State = System.Data.Entity.EntityState.Modified;
            if (db.SaveChanges() > 0)
            {
                return Content("<script>alert('修改成功！');window.location='/home/index'</script>");
            }
            return Content("<script>alert('修改失败！');window.location='/home/index'</script>");

        }
        //退出系统
        public ActionResult exit()
        {
            Session["user"] = null;
            return Content("<script>window.location='/admin/index'</script>");
        }
        //修改密码
        public int upPwd(string jiuPwd, string xinPwd)
        {
            Admin user = (Admin)Session["user"];
            int i = 0;
            if (user.PassWord == jiuPwd)
            {
                user.PassWord = xinPwd;
                db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                i = db.SaveChanges();
                if (i > 0)
                {
                    Session["user"] = null;
                }
            }
            return i;
        }
    }
}
